import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
import Home from "../views/Home.vue";
import Cate from "../views/Cate.vue";
import Register from "../views/register.vue";
import Login from "../views/login.vue";
import Guide from "../views/guide.vue";

const routes: RouteRecordRaw[] = [
    {
        path: '/',
        redirect: '/home'
    },
    {
        path: '/home',
        name: 'home',
        component: Home
    },
    {
        path: '/cate',
        name: 'cate',
        component: Cate
    },
    {
        path: '/register',
        name: 'register',
        component: Register
    },
    {
        path: '/login',
        name: 'login',
        component: Login
    },
    {
        path: '/guide',
        name: 'guide',
        component: Guide
    }
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
});

router.beforeEach((to, from, next) => {
    if (to.path === '/home' || from.path === '/home') {
        const isVisted = localStorage.getItem('isVisted') || 'false';
        if (isVisted === 'true') {
            next(); 
        } else {
            localStorage.setItem('isVisted', 'true');
            next('/guide');
        }
    } else {
        next(); 
    }
});

export default router;